var _currentUrl;
webapp={
	util:{
		isNull:function(s){
			if(!s || s==null || s=="" || s==undefined || s=="null" || s=="NULL"){
				return true;
			}
			return false;
		},
		isNotNull:function(s){
			if(!s || s==null || s=="" || s==undefined || s=="null" || s=="NULL"){
				return false;
			}
			return true;
		},
		fileRename:function(fileName,subfix){
			var s1=fileName.substring(0,fileName.lastIndexOf("."));
			var s2=fileName.substring(fileName.lastIndexOf("."),fileName.length);
			return s1+subfix+s2;
		},
		dateFormat:function(longdate,fmt){
			var d = new Date();
			d.setTime(longdate);
			var o = {
				"M+": d.getMonth() + 1, //月份
				"d+": d.getDate(), //日
				"h+": d.getHours() % 12 == 0 ? 12 : d.getHours() % 12, //小时
				"H+": d.getHours(), //小时
				"m+": d.getMinutes(), //分
				"s+": d.getSeconds(), //秒
				"q+": Math.floor((d.getMonth() + 3) / 3), //季度
				"S": d.getMilliseconds() //毫秒
			};
			var week = {
				"0": "\u65e5",
				"1": "\u4e00",
				"2": "\u4e8c",
				"3": "\u4e09",
				"4": "\u56db",
				"5": "\u4e94",
				"6": "\u516d"
			};
			if(/(y+)/.test(fmt)) {
				fmt = fmt.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
			}
			if(/(E+)/.test(fmt)) {
				fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[d.getDay() + ""]);
			}
			for(var k in o) {
				if(new RegExp("(" + k + ")").test(fmt)) {
					fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
				}
			}
			return fmt;
		},
		randomNumber:function(count){
			var rand = parseInt(Math.random()*count);
		    return rand;
		}
	},
	config:{
		debug:false,
//		restUrl:"http://car.s1.natapp.cc",
		restUrl:"http://118.178.123.130:8080",
//		restUrl:"http://tmliw.free.natapp.cc",
		TOKEN:"token",
		CACHE_USER:"cache_user",
		user_header_img:"user_header_img",
		restPort:"80",
		pageSize:13,
		PULL_DOWN:"down",
		PULL_UP:"up",
		STATUS_BAR_HEIGHT:"StatusbarHeight",
		IS_IMMERSED_STATUSBAR:"isImmersedStatusbar",
		pullRefresh:{
			support:true,
			height:"50px",
			range:"200px",
			contentdown:{
				caption:"下拉刷新"
			},
			contentover:{
				caption:"释放刷新"
			},
			contentrefresh:{
				caption:"正在刷新"
			}
		},
		ANISHOW:"pop-in",
		ANISHOW_DURATION:280
	},
	getStatusbarHeight:function(){
		var _height=window.localStorage.getItem(webapp.config.STATUS_BAR_HEIGHT);
		if(webapp.util.isNull(_height)){
			_height=plus.navigator.getStatusbarHeight();
			window.localStorage.setItem(webapp.config.STATUS_BAR_HEIGHT,_height);
		}
		return _height;
	},
	isImmersedStatusbar:function(){
		var _flag=window.localStorage.getItem(webapp.config.IS_IMMERSED_STATUSBAR);
		if(webapp.util.isNull(_flag)){
			_flag=plus.navigator.isImmersedStatusbar();
			window.localStorage.setItem(webapp.config.IS_IMMERSED_STATUSBAR,_flag);
		}
		return _flag;
	},
	getToken:function(){
		var _token=window.localStorage.getItem(webapp.config.TOKEN);
		if(webapp.util.isNull(_token)){
			_token="";
		}
		return _token;
	},
	getScreenW:function(){
		var _w=window.localStorage.getItem("_W");
		return parseInt(_w);
	},
	//从缓存中查找登录用户信息
	getUser:function(){
		var jsonstr=window.localStorage.getItem(webapp.config.CACHE_USER);
		return JSON.parse(jsonstr);
	},
	showWaiting:function(msg){
		if(msg != undefined && msg.length > 0){ 
			plus.nativeUI.showWaiting( msg,{width:100,height:100,size:10});
		}else{
			plus.nativeUI.showWaiting( "请稍候...",{width:100,height:100,size:10});
		}
	}, 
	closeWaiting:function(){
		plus.nativeUI.closeWaiting();
	},
	debug:function allPrpos ( obj ) {
		if(!webapp.config.debug){
			return false;
		}
		var s=JSON.stringify(obj);
		window.console.log(s);
		return s;
	},
	showLogin:function(){
		//隐藏当前页面
		var loginWebView=plus.webview.getWebviewById("login.html");
		loginWebView.show("slide-in-bottom",700);
		mui.fire(loginWebView,'opened');
	},
	openByTpl:function(con){
		var title=con.title;
		var hrefs=con.hrefs;
		var param=con.param;
		var ricon=con.ricon;
		var defaultW=plus.webview.getWebviewById("common-main");
		var defaultwSub=plus.webview.getWebviewById("common-sub");
		mui.fire(defaultW, 'updateHeader', {
			title:title,
			target:encodeURI(hrefs),
			param:param,
			ricon:ricon
		});
//		if(mui.os.ios||(mui.os.android&&parseFloat(mui.os.version)<4.4)){
//			var reload = true;
//			if (!template.loaded) {
//				if (contentWebview.getURL() != this.href) {
//					contentWebview.loadURL(this.href);
//				} else {
//					reload = false;
//				}
//			} else {
//				reload = false;
//			}
//			(!reload) && contentWebview.show();
//			
//			headerWebview.show("pop-in", 150);
//		}
		
	},
	openByTpl2:function(con){
		var title=con.title;
		var hrefs=con.hrefs;
		var param=con.param;
		var ricon=con.ricon;
		var defaultW=plus.webview.getWebviewById("common2-main");
		var defaultwSub=plus.webview.getWebviewById("common2-sub");
		mui.fire(defaultW, 'updateHeader', {
			title:title,
			target:encodeURI(hrefs),
			param:param,
			ricon:ricon
		});
//		if(mui.os.ios||(mui.os.android&&parseFloat(mui.os.version)<4.4)){
//			var reload = true;
//			if (!template.loaded) {
//				if (contentWebview.getURL() != this.href) {
//					contentWebview.loadURL(this.href);
//				} else {
//					reload = false;
//				}
//			} else {
//				reload = false;
//			}
//			(!reload) && contentWebview.show();
//			
//			headerWebview.show("pop-in", 150);
//		}
	},ajax:function(con){
		//判断网络是否正常
		if(plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE){
			mui.toast("网络连接异常，请检查网络设置！");
			return false;
		}
		
		if(!con.data){
			con.data={};
		}
		var token=webapp.getToken();
		if(!webapp.util.isNull(token)){
			con.data[webapp.config.TOKEN]=token;
		}
		webapp.debug(con);
		mui.ajax(
			webapp.config.restUrl+con.url,
			{
			data    : con.data,
			type    : con.type ? "post" : "get",
			dataType: "json",
			success:function(json){
				if(json.code=="4005"){
//					mui.toast("请登录后再进行该操作");
					webapp.closeWaiting();
					webapp.showLogin();
				}else{
					con.success(json);
				}
			},
			error:function(xhr,type,errorThrown){
				webapp.debug(xhr);
				webapp.debug(errorThrown);
				mui.toast("请求发生错误！"+xhr+type+errorThrown);
			}
		});
	},
	GetPram:function(name){
	     name=name.split("?")[1];
	     return name;
	},
	GetQueryString:function(name){
		if(!_currentUrl){
			_currentUrl=decodeURI(plus.webview.currentWebview().getURL());
			_currentPram=_currentUrl.split("?")[1];
		}
	   	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	    var r = _currentPram.match(reg);
	    if(r!=null)return  unescape(r[2]); return null;
	},
	hideTypewriting:function(arr){
		for(var i=0;i<arr.length;i++){
        	document.getElementById(arr[i]).blur();
    	}
	}
};
